###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/SearchResources.pm.pl -> SearchResources module                #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package CP::SearchResources;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";
require "Authenticate.pm.pl";

use strict;

require "ControlPanel/Output/SearchResources.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "RESOURCES_VIEW");

  #----------------------------------------------------------------------#
  # Getting data...                                                      #

  my ($Categories, $CategoriesIndex) = $in{'DB'}->Query(
    TABLE   => "Categories",
    SORT    => "FULL_TITLE",
    BY      => "A-Z"
  );

  $Categories = &Authenticate::FilterCategories(CATEGORIES => $Categories, ACTION => "RESOURCES_VIEW");
  
  my %INPUT;
  
  $INPUT{'CATEGORIES'} = $Categories;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::SearchResources->new();

  &Standard::PrintHTMLHeader();
  print $Skin->show(input => \%INPUT);
  
  return 1;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (DB => undef, @_);

  #----------------------------------------------------------------------#
  # Authenticating...                                                    #
  
  &Authenticate::CP(DB => $in{'DB'}, ACTION => "RESOURCES_VIEW");

  #----------------------------------------------------------------------#
  # Getting data...                                                      #
  
  my ($Categories, $CategoriesIndex) = $in{'DB'}->Query(
    TABLE   => "Categories",
    SORT    => "FULL_TITLE",
    BY      => "A-Z"
  );

  $SL::QUERY{'Sort'}    ||= "TITLE";
  $SL::QUERY{'By'}      ||= "A-Z";
  $SL::QUERY{'Page'}    ||= 1;
  $SL::QUERY{'PerPage'} ||= 10;

  my %Where;

  foreach my $field ("TITLE", "URL", "DESCRIPTION", "KEYWORDS", "CONTACT_NAME", "CONTACT_EMAIL") {
    next unless ($SL::QUERY{'FORM_'.$field});
    
    $Where{$field} = [$SL::QUERY{'FORM_'.$field}];
    $Where{$field} = ["=~".$SL::QUERY{'FORM_'.$field}] if ($SL::QUERY{'FORM_'.$field.'_TYPE'} eq "CONTAINS");
  }

  if ($SL::QUERY{'FORM_CREATE'}) {
    if ($SL::QUERY{'FORM_CREATE_TYPE'} eq "LESS") {
      $Where{'CREATE_SECOND'} = [">".(time - ($SL::QUERY{'FORM_CREATE'} * 24 * 60 * 60))];
    } else {
      $Where{'CREATE_SECOND'} = ["<".(time - ($SL::QUERY{'FORM_CREATE'} * 24 * 60 * 60))];
    }
  }

  if ($SL::QUERY{'FORM_UPDATE'}) {
    if ($SL::QUERY{'FORM_UPDATE_TYPE'} eq "LESS") {
      $Where{'UPDATE_SECOND'} = [">".(time - ($SL::QUERY{'FORM_UPDATE'} * 24 * 60 * 60))];
    } else {
      $Where{'UPDATE_SECOND'} = ["<".(time - ($SL::QUERY{'FORM_UPDATE'} * 24 * 60 * 60))];
    }
  }

  my @Categories = $SL::CGI->param('FORM_CATEGORY');
  my @Status     = $SL::CGI->param('FORM_STATUS');
  my @Flags      = $SL::CGI->param('FORM_FLAGS');

  &Authenticate::CheckPermission(ACTION => "RESOURCES_VIEW", CATEGORIES => \@Categories);
  
  unless (scalar(@Categories) >= 1) {
    my $categories = &Authenticate::FilterCategories(CATEGORIES => $Categories, ACTION => "RESOURCES_VIEW");
    @Categories = &Standard::MapArray("\${ID}", $categories);
  }

  my $categories = &Standard::ArrayToHash(\@Categories);
  my $status     = &Standard::ArrayToHash(\@Status);
  my $flags      = &Standard::ArrayToHash(\@Flags);

  my ($Resources, undef) = $in{'DB'}->Query(
    TABLE   => "Resources",
    WHERE   => \%Where,
    MATCH   => "ALL",
    SORT    => $SL::QUERY{'Sort'},
    BY      => $SL::QUERY{'By'}
  );
      
  my @Resources;
      
  foreach my $resource (@{ $Resources }) {
    next if (!$categories->{ $resource->{'CATEGORY'} });
    next if ($SL::QUERY{'FORM_STATUS'} ne "" && !$status->{ $resource->{'STATUS'} });
      
    if ($SL::QUERY{'FORM_FLAGS'}) {
      my @flags = split(/,/, $resource->{'FLAGS'});
      
      my $found;
      foreach my $flag (@flags) {
        ($found = 1 and last) if ($flags->{ $flag });
      }
      
      next unless ($found);
    }
      
    push(@Resources, $resource);
  }
      
  my $Pages = &Standard::GetPages($SL::QUERY{'Page'}, scalar(@Resources), $SL::QUERY{'PerPage'});
     
  @Resources = @Resources[$Pages->{'start'}..$Pages->{'finish'}];
  $Resources = \@Resources;

  if ($SL::QUERY{'FORM_TYPE'} eq "CHECK") {
    require "LinkChecker.pm.pl";
    my $LinkChecker = LinkChecker->new(timeout => $SL::QUERY{'FORM_TIMEOUT'});
    
    foreach my $resource (@{ $Resources }) {
      my ($code, $message, $pass) = $LinkChecker->Check($resource->{'URL'});
      $resource->{'check'}->{'CODE'}    = $code;
      $resource->{'check'}->{'MESSAGE'} = $message;
      $resource->{'check'}->{'PASS'}    = $pass;
    }
  }

  my %INPUT;
  
  $INPUT{'RESOURCES'}     = $Resources;
  $INPUT{'PAGES'}         = $Pages;
  $INPUT{'CATEGORIES'}    = $Categories;
  $INPUT{'CATEGORIES_IX'} = $CategoriesIndex;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::CP::SearchResources->new();

  &Standard::PrintHTMLHeader();
  print $Skin->do(input => \%INPUT);
  
  return 1;
}

1;